package com.net.oil.order.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.order.MaintainDetailActivity;
import com.net.oil.order.bean.MaintainListBean;
import com.net.oil.util.DateFormat;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2018/6/7 0007:下午 21:16
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class MaintainRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<MaintainListBean.ListBean> list;

    private static final int TYPE_FOOTER = 0;  //底部FootView
    private static final int TYPE_ITEM = 1;  //普通item

    private int load_more_status = 10;      //上拉加载更多状态-默认为10

    public MaintainRecyclerViewAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void updateData(List<MaintainListBean.ListBean> list){
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void loadMore(List<MaintainListBean.ListBean> list){
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM:
                view = inflater.inflate(R.layout.maintain_recycler_view_item, parent, false);
                return new ViewHolder(view);
            case TYPE_FOOTER:
                view = inflater.inflate(R.layout.recycler_view_foot_item, parent, false);
                return new FootViewHolder(view);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ViewHolder){
            ViewHolder viewHolder = (ViewHolder) holder;
            final MaintainListBean.ListBean listBean = list.get(position);
            if (listBean != null){
                if (!TextUtils.isEmpty(listBean.getBusinessName())){
                    viewHolder.maintainShopTv.setText(listBean.getBusinessName());
                }
                viewHolder.maintainTimeTv.setText(DateFormat.formatDateTime(listBean.getT(), DateFormat.FormatFlag.YDMHM));
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, MaintainDetailActivity.class);
                        intent.putExtra(Constant.INTENT_RECORD_ID, listBean.getId());
                        context.startActivity(intent);
                    }
                });
                if (listBean.getApplimentCode() == null){
                    viewHolder.isUseCouponImg.setVisibility(View.GONE);
                }else {
                    viewHolder.isUseCouponImg.setVisibility(View.VISIBLE);
                }
                if (listBean.getCommentStatus() == 1){
                    viewHolder.isCommentTv.setText("已评论");
                }else if (listBean.getCommentStatus() == 0){
                    viewHolder.isCommentTv.setText("未评论");
                }
            }
        }else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footDataBind(footViewHolder);
        }


    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size() + 1;
    }

    /**
     * 更改滑动的状态
     *
     * @param status 上拉加载更多 PULL_LOAD_MORE = 10; 正在加载中 LOADING_MORE = 11;
     *               没有更多 LOADING_NONE = 12; 上拉加载更多状态-默认为10
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }

    /**
     * 绑定加载更多项的数据
     *
     * @param footViewHolder holder
     */
    private void footDataBind(FootViewHolder footViewHolder) {
        switch (load_more_status) {
            case Constant.PULL_LOAD_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("上拉加载更多...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("正在加载更多数据...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.VISIBLE);
                break;
            case Constant.LOADING_NONE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("没有更多了");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_FAIL:
                footViewHolder.more.setText("加载失败");
                footViewHolder.more.setEnabled(true);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.maintain_shop_tv)
        TextView maintainShopTv;
        @BindView(R.id.maintain_time_tv)
        TextView maintainTimeTv;
        @BindView(R.id.is_comment_tv)
        TextView isCommentTv;
        @BindView(R.id.is_use_coupon_img)
        ImageView isUseCouponImg;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.progress_bar)
        ProgressBar progressBar;
        @BindView(R.id.more)
        TextView more;

        FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
